<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="账户名称" prop="accountName">
        <el-input
          v-model="queryParams.accountName"
          placeholder="请输入账户名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="accountList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="账户名称" align="center" prop="accountName" />
      <el-table-column label="账户类型" align="center" prop="accountType">
        <template #default="scope">
          <dict-tag :options="accountTypeOptions" :value="scope.row.accountType" />
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="contactPhone" />
      <el-table-column label="银行账号" align="center" prop="bankAccount" />
      <el-table-column label="所属公寓" align="center" prop="apartmentName" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template #default="scope">
          <dict-tag :options="auditStatusOptions" :value="scope.row.auditStatus" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="statusOptions" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            size="mini"
            @click="handleAudit(scope.row)"
            v-if="scope.row.auditStatus === '0'"
          >审核</el-button>
          <el-button
            link
            type="primary"
            icon="Check"
            size="mini"
            @click="handleStatusChange(scope.row)"
            v-if="scope.row.auditStatus === '1'"
          >{{ scope.row.status === '0' ? '停用' : '启用' }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 使用独立的审核对话框组件 -->
    <audit-dialog
      v-model:visible="auditOpen"
      :data="form"
      :dictOptions="dictOptions"
      @submit="onAuditSubmit"
      @close="onAuditClose"
    />
  </div>
</template>

<script setup name="PaymentAccount">
import { ref, reactive, toRefs, getCurrentInstance, computed } from 'vue';
import { listPaymentAccount, getPaymentAccount, auditPaymentAccount, changePaymentAccountStatus } from "@/api/apartment/payment";
// 导入审核对话框组件
import AuditDialog from './components/AuditDialog.vue';

const { proxy } = getCurrentInstance();
const accountList = ref([]);
const auditOpen = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

// 使用useDict获取字典数据
const { 
  payment_account_type: accountTypeOptions, 
  payment_audit_status: auditStatusOptions, 
  payment_account_status: statusOptions,
  payment_settlement_type: settlementTypeOptions
} = proxy.useDict(
  "payment_account_type", 
  "payment_audit_status", 
  "payment_account_status",
  "payment_settlement_type"
);

// 字典选项，用于传递给组件
const dictOptions = computed(() => {
  return {
    accountTypeOptions,
    settlementTypeOptions
  }
});

const data = reactive({
  form: {
    accountId: undefined,
    accountName: undefined,
    accountType: undefined,
    contactPhone: undefined,
    contactEmail: undefined,
    bankName: undefined,
    bankAccount: undefined,
    accountHolder: undefined,
    settlementType: undefined,
    companyName: undefined,
    unifiedSocialCreditCode: undefined,
    legalPersonName: undefined,
    legalPersonIdCardNo: undefined,
    businessLicenseImg: undefined,
    legalPersonIdCardFrontImg: undefined,
    legalPersonIdCardBackImg: undefined,
    accountOpeningLicenseImg: undefined,
    businessPlaceImg: undefined,
    auditStatus: undefined,
    auditRemark: undefined,
    status: undefined,
    apartmentId: undefined,
    apartmentName: undefined,
    idCardNo: undefined,
    idCardFrontImg: undefined,
    idCardBackImg: undefined
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    accountName: undefined,
    contactPhone: undefined
  }
});

const { queryParams, form } = toRefs(data);

/** 查询收款账户列表 */
function getList() {
  loading.value = true;
  listPaymentAccount(queryParams.value).then(response => {
    accountList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.accountId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 审核按钮操作 */
function handleAudit(row) {
  getPaymentAccount(row.accountId).then(response => {
    form.value = response.data;
    auditOpen.value = true;
  });
}

/** 审核提交回调 */
function onAuditSubmit(data) {
  auditPaymentAccount(data).then(response => {
    proxy.$modal.msgSuccess("审核成功");
    auditOpen.value = false;
    getList();
  });
}

/** 审核对话框关闭回调 */
function onAuditClose() {
  form.value = {
    accountId: undefined,
    accountName: undefined,
    accountType: undefined,
    contactPhone: undefined,
    contactEmail: undefined,
    bankName: undefined,
    bankAccount: undefined,
    accountHolder: undefined,
    settlementType: undefined,
    companyName: undefined,
    unifiedSocialCreditCode: undefined,
    legalPersonName: undefined,
    legalPersonIdCardNo: undefined,
    businessLicenseImg: undefined,
    legalPersonIdCardFrontImg: undefined,
    legalPersonIdCardBackImg: undefined,
    accountOpeningLicenseImg: undefined,
    businessPlaceImg: undefined,
    auditStatus: undefined,
    auditRemark: undefined,
    status: undefined,
    apartmentId: undefined,
    apartmentName: undefined,
    idCardNo: undefined,
    idCardFrontImg: undefined,
    idCardBackImg: undefined
  };
  auditOpen.value = false;
}

/** 修改状态 */
function handleStatusChange(row) {
  let text = row.status === "0" ? "停用" : "启用";
  proxy.$modal.confirm('确认要"' + text + '""' + row.accountName + '"收款账户吗?').then(function() {
    return changePaymentAccountStatus({
      accountId: row.accountId,
      status: row.status === "0" ? "1" : "0"
    });
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(() => {});
}

getList();
</script> 